//
//  HOTDetailsViewController.m
//  wedding
//
//  Created by 常宽 on 15/1/20.
//  Copyright (c) 2015年 常宽. All rights reserved.
//

#import "HOTDetailsViewController.h"
#import "MoreViewController.h"      // 用来判断上级页面
#import "HptDetailsCell.h"          // 自定义的图文混编cell
#import "HomePageModel.h"           // 传进来的model类
#import "OptionlistModel.h"         // 基本信息model类
#import "PostlistModel.h"           // cell的model类

@interface HOTDetailsViewController ()<UITableViewDataSource, UITableViewDelegate>
///  数据数组
@property (nonatomic, retain)NSMutableArray *dataArray;
///  各种选项
@property (nonatomic, retain)OptionlistModel *option;
///
@property (nonatomic, retain)UITableView *tableView;
@end

@implementation HOTDetailsViewController

- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.HPmodel = [[HomePageModel alloc] init];
        self.dataArray = [[NSMutableArray alloc] init];
        self.option = [[OptionlistModel alloc] init];
    }
    return self;
}

#pragma mark 网络请求
- (void) netWorking
{
    NSString *detailsUrl = [NSString stringWithFormat:@"http://circle.halobear.cn/api/mobile/index.php?module=viewthread&charset=utf-8&image=1&ppp=1000&version=3&page=1&tid=%@", self.HPmodel.tid];
    [NetHandler networkWithURL:detailsUrl completion:^(id result) {
        NSDictionary *bigDic = [result objectForKey:@"Variables"];
        
        
        [_option setValuesForKeysWithDictionary:[bigDic objectForKey:@"optionlist"]];
        NSArray *array = [bigDic objectForKey:@"postlist"];
        for (NSDictionary *dic in array) {
            PostlistModel *model = [[PostlistModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        
        [self.tableView reloadData];
    }];
//    NSLog(@"%@", detailsUrl);
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 二级页面隐藏tabBar
    self.tabBarController.tabBar.hidden = YES;
    // 重写返回上级页面方法
    UIImage *backImage = [[UIImage imageNamed:@"返回.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem= [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(popBack:)];
    [self netWorking];
    [self createTableView];
}

// 返回上级页面
- (void)popBack:(UIBarButtonItem *)sender
{
    [self.tabBarController.tabBar setHidden:NO];
    NSArray *controllers = self.navigationController.viewControllers;    
    if ([[controllers objectAtIndex:controllers.count - 2] isKindOfClass:[MoreViewController class]]) {
        MoreViewController *controller = [controllers objectAtIndex:controllers.count - 2];
        [self.navigationController.navigationBar addSubview:controller.segCon];
    }
        
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mrak 创建表视图
- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}

#pragma mark -
#pragma mark tabelView协议
///  section 个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
///  每个section里的row的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
///  返回tableView里的cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string = @"Postlist";
    HptDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[HptDetailsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    PostlistModel *hpModel = [self.dataArray objectAtIndex:indexPath.row];
    [cell.picView sd_setImageWithURL:[NSURL URLWithString:[[hpModel.images lastObject] objectForKey:@"url"]] placeholderImage:[UIImage imageNamed:@"NoPicture.jpg"]];
    return cell;
}
/// 返回row的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // UIImageView自适应
    
//    HptDetailsCell *cell = (HptDetailsCell *)[tableView cellForRowAtIndexPath:indexPath];
//    cell.picView.image
//    CGSize size = cell.picView.image.size;
    
    UIImageView *img = [[UIImageView alloc]init];
    PostlistModel *hpModel = [self.dataArray objectAtIndex:indexPath.row];
    
    [img sd_setImageWithURL:[NSURL URLWithString:[[hpModel.images lastObject] objectForKey:@"url"]] placeholderImage:[UIImage imageNamed:@"NoPicture.jpg"]];
    
    
    
    UIImage *aImage = img.image;
    CGSize size = aImage.size;
    
    CGFloat w = _tableView.frame.size.width + 10;
    float scale = size.width / w;
    float height = size.height /scale;
    return height;
}
///  选中cell执行的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
